package com.harisucici.websocketlib.utils;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.harisucici.websocketlib.entities.receive.CurrentSessionReceive;
import com.harisucici.websocketlib.entities.receive.MyFriends;
import com.harisucici.websocketlib.entities.receive.SessionListReceive;
import com.harisucici.websocketlib.entities.send.P2PSend;
import com.harisucici.websocketlib.entities.send.P2PSendHoleEntity;
import com.harisucici.websocketlib.modle.BaseSendEntity;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 一般工具类
 * Created by harisucici on 2017/6/13.
 */

public class Utils<T> {
    private static Utils utils;

    public static synchronized Utils getInstance() {
        if (utils == null) {
            utils = new Utils();
        }
        return utils;
    }


    // 根据完整路径获得类
    public Class getClass(String s) {
        Class c = null;
        try {
            c = Class.forName(s);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        return c;
    }


    // 根据类名生成对象
    public Object getObject(Class c) {
        Object o = null;
        try {
            o = c.newInstance();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return o;
    }

    public Map<String, String> jso2map(String s) {
        JSONObject jsonObjTemp;
        Map<String, String> map = new HashMap<>(2);
        if (s == null) {
            return null;
        }
        try {
            jsonObjTemp = new JSONObject(s);
            Iterator ite = jsonObjTemp.keys();
            while (ite.hasNext()) {
                String key = ite.next().toString();
                String value = jsonObjTemp.get(key).toString();
                map.put(key, value);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return map;
    }




    @Deprecated
    public BaseSendEntity buildSend(String cmdType, String fromAccount, String toAccount) {
        BaseSendEntity baseEntity = new BaseSendEntity();
        baseEntity.setCmdType(cmdType);
        BaseSendEntity.FromAccountEntity fromAccountEntity = new BaseSendEntity.FromAccountEntity();
        fromAccountEntity.setAccountId(fromAccount);
        BaseSendEntity.ToAccountEntity toAccountEntity = new BaseSendEntity.ToAccountEntity();
        toAccountEntity.setAccountId(toAccount);
        baseEntity.setFromAccount(fromAccountEntity);
        baseEntity.setToAccount(toAccountEntity);

        return baseEntity;
    }

    public P2PSendHoleEntity buildSendP2P(String cmdType, String fromAccount, String toAccount) {

        P2PSendHoleEntity baseEntity = new P2PSendHoleEntity();
        baseEntity.setCmdType(cmdType);
        P2PSendHoleEntity.FromAccountEntity fromAccountEntity = new P2PSendHoleEntity.FromAccountEntity();
        fromAccountEntity.setAccountId(fromAccount);
        P2PSendHoleEntity.ToAccountEntity toAccountEntity = new P2PSendHoleEntity.ToAccountEntity();
        toAccountEntity.setAccountId(toAccount);
        baseEntity.setFromAccount(fromAccountEntity);
        baseEntity.setToAccount(toAccountEntity);

        return baseEntity;
    }


    public void deleteDB(Context context) {
        File databases = new File(context.getApplicationInfo().dataDir + "/databases");
        File dbCS = new File(databases, "current_session");
        File dbMF = new File(databases, "my_friend");
        File dbP2P = new File(databases, "p2p_msg_unsend");
        File dbSL = new File(databases, "session_list");
        if (dbCS.delete()) {
            Log.i(Constants.API, "数据删除成功CS");
        } else {
            Log.i(Constants.API, "数据删除失败CS");
        }
        if (dbMF.delete()) {
            Log.i(Constants.API, "数据删除成功MF");
        } else {
            Log.i(Constants.API, "数据删除失败MF");
        }
        if (dbP2P.delete()) {
            Log.i(Constants.API, "数据删除成功P2P");
        } else {
            Log.i(Constants.API, "数据删除失败P2P");
        }
        if (dbSL.delete()) {
            Log.i(Constants.API, "数据删除成功SL");
        } else {
            Log.i(Constants.API, "数据删除失败SL");
        }

    }

    public long makeAppSeqId() {
        long appSeqId = 0;
        appSeqId = System.currentTimeMillis();
        return appSeqId;
    }

    public CurrentSessionReceive p2c(P2PSendHoleEntity p2PSendHoleEntity, T t) {
        CurrentSessionReceive cs = new CurrentSessionReceive();
        cs.setMsgType(Constants.TEXT);
        if (t instanceof SessionListReceive) {
            //在这取头像是错的
//            cs.setImgAvator(((SessionListReceive) t).getImgAvator());
//            cs.setTextAvator(((SessionListReceive) t).getTextAvator());
            if (TextUtils.isEmpty(((SessionListReceive) t).getUserId())) {
                cs.setType("GROUP");
            }
        } else if (t instanceof MyFriends) {
            //在这取头像是错的
//            cs.setImgAvator(((MyFriends) t).getImgAvator());
//            cs.setTextAvator(((MyFriends) t).getTextAvator());
            if (TextUtils.isEmpty(((MyFriends) t).getUserId())) {
                cs.setType("GROUP");
            }
        }

        cs.setTimelineId(p2PSendHoleEntity.getPayLoad().getTimelineId());
        cs.setType("SINGLE");

        cs.setAppSeqId(p2PSendHoleEntity.getPayLoad().getAppSeqId());


        setCSContent(cs,p2PSendHoleEntity.getPayLoad());
//        cs.setMsgType(p2PSendHoleEntity.getPayLoad().getMsgType());
//        cs.setText(p2PSendHoleEntity.getPayLoad().getContent());

        cs.setTimestamp(p2PSendHoleEntity.getPayLoad().getTimestamp());

        cs.setSender(p2PSendHoleEntity.getFromAccount().getAccountId());
        return cs;
    }

    // 发送消息内容物设置
    public void setCSContent(CurrentSessionReceive cs, P2PSend ps) {
        cs.setMsgType(ps.getMsgType());
        if (ps.getMsgType().equals(Constants.TEXT)) {
            cs.setText(ps.getContent());
        } else if (ps.getMsgType().equals(Constants.IMAGE)) {
            cs.setText(ps.getImage());
        } else if (ps.getMsgType().equals(Constants.VIDEO)) {
            cs.setText(ps.getVideo());
            cs.setTime(ps.getTime());
        } else if (ps.getMsgType().equals(Constants.SYSTEM)) {
            cs.setText(ps.getSystem());
        } else if (ps.getMsgType().equals(Constants.AUDIO)) {
            cs.setText(ps.getAudio());
            cs.setTime(ps.getTime());
        } else if (ps.getMsgType().equals(Constants.LINK)) {
            cs.setText(ps.getLink());
        } else {
            cs.setText(ps.getNormal());
        }


    }

    public SessionListReceive c2s(CurrentSessionReceive cs) {

        SessionListReceive sl = new SessionListReceive();
//        sl.setUserNo(cs);
        sl.setLastMessage(cs.getText());
        sl.setMsgType(cs.getMsgType());
        sl.setLastMessageSeqId(cs.getSequenceId());
//        sl.setUnReadNum(cs.);
        sl.setTextAvator(cs.getTextAvator());
//        sl.setCreateTime(cs.);
        sl.setName(cs.getSenderName());
        sl.setUserId(String.valueOf(cs.getId()));
        sl.setTimelineId(cs.getTimelineId());
//        sl.setTop(cs);
        sl.setType(cs.getType());
        sl.setImgAvator(cs.getImgAvator());
//        sl.setRemove(cs);
        sl.setTimestamp(cs.getTimestamp());
//        sl.setSendState(Constants.SENDED);
        return sl;

    }


//    public SessionListReceive p2s(P2PSendHoleEntity p2PSendHoleEntity, T t) {
//        SessionListReceive sl = new SessionListReceive();
//        if (t instanceof MyFriends) {
//            // todo 联系人进入
//        } else if (t instanceof SessionListReceive) {
//            sl = (SessionListReceive) t;
//            // 最近会话进入
//            sl.setLastMessage(p2PSendHoleEntity.getPayLoad().getContent());
//            sl.setTimestamp(p2PSendHoleEntity.getPayLoad().getTimestamp());
//        }
//        return sl;
//    }

}

